﻿using System;
using ASPNETVN.PORTAL.Components.Security;
using System.Web.UI;

namespace ASPNETVN.PORTAL.Controls
{
    public partial class Register : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Random rnd = new Random();
                int digit1 = rnd.Next(0, 9);
                imgNumber1.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit1);

                int digit2 = rnd.Next(0, 9);
                imgNumber2.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit2);

                int digit3 = rnd.Next(0, 9);
                imgNumber3.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit3);

                int digit4 = rnd.Next(0, 9);
                imgNumber4.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit4);

                ViewState["Digit"] = digit1 + "" + digit2 + "" + digit3 + "" + digit4;
            }
        }

        protected void btnSignIn_Click(object sender, EventArgs e)
        {
            if (chkAgree.Checked)
            {
                try
                {
                    if (ViewState["Digit"].ToString() == txtSecurityCode.Text)
                    {
                        User user = new User();
                        user.Name = txtName.Text;
                        user.Username = txtUsername.Text.Trim().ToLower();
                        user.Email = txtEmail.Text.Trim().ToLower();
                        user.Password = txtPassword.Text;
                        int returnID = user.Register();
                        if (returnID > 0)
                        {
                            Response.Redirect("~/RegisterCompleted.aspx");
                        }
                        else if (returnID == -1)
                        {
                            txtMessage.Text = "This email has been exists. Please try again!";                            
                        }
                        else if (returnID == -2)
                        {
                            txtMessage.Text = "This username has been exists. Please try again!";
                        }
                    }
                    else
                    {
                        txtSecurityCode.Text = string.Empty;
                        txtMessage.Text = "Check code wrong!";
                        Random rnd = new Random();
                        int digit1 = rnd.Next(0, 9);
                        imgNumber1.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit1);

                        int digit2 = rnd.Next(0, 9);
                        imgNumber2.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit2);

                        int digit3 = rnd.Next(0, 9);
                        imgNumber3.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit3);

                        int digit4 = rnd.Next(0, 9);
                        imgNumber4.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit4);

                        ViewState["Digit"] = digit1 + "" + digit2 + "" + digit3 + "" + digit4;
                    }
                }
                catch (Exception ex)
                {
                    txtMessage.Text = "System failed: " + ex.Message;
                }
            }
            else
            {
                txtMessage.Text = "Please choose I agree.";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Default.aspx");
        }
    }
}